//
freeslot(
"MT_FAKE_STARPOST",
"S_FAKE_STARPOST"
)

mobjinfo[MT_FAKE_STARPOST] = {
	doomednum = -1,
	spawnstate = S_FAKE_STARPOST,
	spawnhealth = 1000,
	reactiontime = 2,
	radius = 1*FRACUNIT,
	height = 1*FRACUNIT,
	mass = 12,
	dispoffset = 1,
	flags = MF_SCENERY
}

states[S_FAKE_STARPOST] = {SPR_STPT, A, -1, nil, 2, 2, S_NULL}

//------ functions for single player------------//
local MetalCircuitStart = function(player)
	local mo = player.mo
	P_LinedefExecute(510, mo,  mo.subsector.sector)
end

local MetalCircuitWrongWay = function(player,damage)
	// if damage is true hurt the player beacuse thay fell into a pit
	local mo = player.mo
	mo.momx = 0
	mo.momy = 0
	// telport player to there last check point
	if player.metalrace == 2 or player.starpostnum == 1 then
		P_SetOrigin(mo, -1440*FRACUNIT, 3680*FRACUNIT, 787*FRACUNIT)
		mo.angle = ANGLE_270
	elseif player.metalrace == 3 or player.starpostnum == 2 then
		P_SetOrigin(mo, 2432*FRACUNIT, 320*FRACUNIT, -12*FRACUNIT)
		mo.angle = ANG1
	elseif player.metalrace == 4  or player.starpostnum == 3 then
		P_SetOrigin(mo, 8416*FRACUNIT, -2304*FRACUNIT, 2232*FRACUNIT)
		mo.angle = ANG255
	else  // assume there at the start
		P_SetOrigin(mo, 256*FRACUNIT, -1056*FRACUNIT, 648*FRACUNIT)
		mo.angle = ANGLE_90
	end
	if damage then
		P_DamageMobj(mo, nil, nil, 1, 0)
	else
		S_StartSound(mo, sfx_lose, player)
	end
end

addHook("MobjSpawn", function(mo)
if not mapheaderinfo[gamemap].rainfoliage then return end
if (gametype > 1) then 
	P_RemoveMobj(mo)
return end
	mo.racecountdown = 143*TICRATE  //194*TICRATE 
	mo.metallost = false
end, MT_METALSONIC_RACE)

addHook("MobjThinker", function(metal) // have metal himself control the time limit
if not mapheaderinfo[gamemap].rainfoliage then return end
if (gametype > 1) then return end
	local countdown = false
	local racetime = metal.racecountdown
	local mlost = metal.metallost
	if metal.racecountdown == 129*TICRATE then // make crumble fof fall if it hasent
		P_LinedefExecute(114, metal,  metal.subsector.sector)
	end
	for player in players.iterate do
		if player.metalrace then
			countdown = true
		end

		if player.metalracelap and player.metalracelap > 3 then
		  mlost = true
		end
		player.metalcountdown = racetime
		if racetime < 0 and not mlost then //make sure it dont trigger when a player has finished
			P_DamageMobj(player.mo, nil, nil, 1, 128) // instant kill
			G_SetCustomExitVars(gamemap, 2) //map number to its self
		end
		if racetime <= -1*TICRATE then // restarts the map should only happen in co-op....
			G_ExitLevel()
		end
	end
	if countdown and not mlost then
		metal.racecountdown = $-1
	end
	if mlost then
		metal.metallost = true
	end
	
end, MT_METALSONIC_RACE)

addHook("PlayerSpawn", function(player)
	if not mapheaderinfo[gamemap].rainfoliage then return end
	player.metalrace = 0
	player.metalracelap = 1
	player.metalcountdown = 193*TICRATE
end)

addHook("MapThingSpawn", function(mo, thing) //dont want starposts in singleplayer so make fake ones instead!
	if not mapheaderinfo[gamemap].rainfoliage then return end
	if (gametype > 1) then return end
	P_RemoveMobj(mo)
end, MT_STARPOST)

addHook("MobjRemoved", function(mo)
	if not mapheaderinfo[gamemap].rainfoliage then return end
	if (gametype > 1) then return end
	local fake = P_SpawnMobj(mo.x, mo.y, mo.z, MT_FAKE_STARPOST)
	fake.colorized = true 
	fake.color = SKINCOLOR_APPLE
end, MT_STARPOST)

// makeing the hitbox on blue springs taller because there too damn short
addHook("MobjSpawn", function(mo)
if not mapheaderinfo[gamemap].rainfoliage then return end
	mo.height = $*2
end, MT_BLUEDIAG)

//--------- single player race lua linedef trigger----------//

local function raceintopit(line, mo)
	if mo and mo.player then
		MetalCircuitWrongWay (mo.player,true) // do damage you fell in a pit
	end
end

addHook("LinedefExecute", raceintopit, "MRACETR0")

local function racefinishline(line, mo)
	if (gametype > 1) then return end
	if mo and mo.player then
	
		if mo.player.metalrace == 0 then // first lap
		mo.player.metalrace = 1
		MetalCircuitStart(mo.player)
		elseif mo.player.metalrace > 3 then
		mo.player.metalrace = 1
		S_StartSound(mo, sfx_strpst, player)
		mo.player.metalracelap = $+1
			if mo.player.metalracelap > 3 then
				mo.player.metalracelap = 4
				P_DoPlayerFinish(mo.player)//finish level here//
			end
		elseif mo.player.metalrace > 1 then
			print(mo.player.metalrace)
			MetalCircuitWrongWay (mo.player,false) // dont damage
		end
	end
end

addHook("LinedefExecute", racefinishline, "MRACETR1")

local function racecheckpoint1(line, mo)
	if (gametype > 1) then return end
	if mo and mo.player then
		if mo.player.metalrace == 1 then
		mo.player.metalrace = 2
		S_StartSound(mo, sfx_strpst, player)
		elseif mo.player.metalrace != 2 then
			MetalCircuitWrongWay (mo.player,false) // dont damage
		end
	end
end

addHook("LinedefExecute", racecheckpoint1, "MRACETR2")

local function racecheckpoint2(line, mo)
	if (gametype > 1) then return end
	if mo and mo.player then
		if mo.player.metalrace == 2 then
		mo.player.metalrace = 3
		S_StartSound(mo, sfx_strpst, player)
		elseif mo.player.metalrace != 3 then
			MetalCircuitWrongWay (mo.player,false) // dont damage
		end
	end
end

addHook("LinedefExecute", racecheckpoint2, "MRACETR3")

local function racecheckpoint3(line, mo)
	if (gametype > 1) then return end
	if mo and mo.player then
		if mo.player.metalrace == 3 then
		mo.player.metalrace = 4
		S_StartSound(mo, sfx_strpst, player)
		elseif mo.player.metalrace != 4 then
			MetalCircuitWrongWay (mo.player,false) // dont damage
		end
	end
end

addHook("LinedefExecute", racecheckpoint3, "MRACETR4")

// normal circuit lua I use//

local function tradeshield(line, mo) // trade shield for rings
		if (mo.player.powers[pw_shield] & SH_NOSTACK) == SH_ARMAGEDDON then
			P_GivePlayerRings(mo.player, 200)
			S_StartSound(mo, sfx_chchng, mo.player)
			P_SwitchShield(mo.player, SH_NONE)
		elseif (mo.player.powers[pw_shield] & SH_NOSTACK) == SH_FORCE|1 then
			P_GivePlayerRings(mo.player, 50)
			S_StartSound(mo, sfx_chchng, mo.player)
			P_SwitchShield(mo.player, SH_NONE)
		elseif (mo.player.powers[pw_shield]) > 0 then
			P_GivePlayerRings(mo.player, 20)
			S_StartSound(mo, sfx_chchng, mo.player)
			P_SwitchShield(mo.player, SH_NONE)
		end
	
end

addHook("LinedefExecute", tradeshield, "LUATRA")

local function fourcesuperspeed(line, mo) // spend rings for super shoes
		if mo.player.rings >= 21 then
			mo.player.rings = $-21
			mo.player.powers[pw_sneakers] = TICRATE*25
			S_StartSound(mo, sfx_antiri, mo.player)
		end
	
end

addHook("LinedefExecute", fourcesuperspeed, "LUASPE") 

local function fourcegravityboots(line, mo) // spend rings for gravity
		if mo.player.rings >= 34 then
			mo.player.rings = $-34
			mo.momz = 0
			mo.player.powers[pw_gravityboots] = TICRATE*3/2
			S_StartSound(mo, sfx_antiri, mo.player)
		end
	
end

addHook("LinedefExecute", fourcegravityboots, "LUAGRV") 

local function fourcerecycle(line, mo) // spend rings for Recycler
	
		if mo.valid and mo.player.rings >= 5 then
			mo.player.rings = $-5
			local icon = P_SpawnMobj(mo.x, mo.y, mo.z, MT_RECYCLER_ICON)
			S_StartSound(mo, sfx_antiri, mo.player)
			icon.flags2 = $ | MF2_DONTDRAW
		end
	
end

addHook("LinedefExecute", fourcerecycle, "LUARYC") 

local function fourcewindshield(line, mo) // spend rings for wind shield
	
		if mo.valid and mo.player.rings >= 21 then
			mo.player.rings = $-21
			local icon = P_SpawnMobj(mo.x, mo.y, mo.z, MT_WHIRLWIND_ICON)
			icon.target = mo
			icon.flags2 = $ | MF2_DONTDRAW
			S_StartSound(mo, sfx_antiri, mo.player)
		end
	
end

addHook("LinedefExecute", fourcewindshield, "LUAWIND") 

